package br.com.merks.controller.component;

import org.apache.log4j.Logger;

import br.com.caelum.vraptor.ioc.Component;
import br.com.merks.model.User;
import br.com.merks.service.UserService;
import br.com.yx.vraptor.security.SecurityHandler;
@Component
public class SecurityModule implements SecurityHandler<User> {
		public static final Logger logger = Logger.getLogger(SecurityModule.class);
	
	   
	     private UserService userService;
	     private UserSessionHandler userSessionHandler;
	     
	     public SecurityModule(UserService userService, UserSessionHandler userSessionHandler) {
			    this.userService = userService;
			    this.userSessionHandler = userSessionHandler;
	     }
	    
	     public void setUser(User objeto) {
	    	this.userSessionHandler.setUser( objeto );
	      
	     }
	     public User getUser() {
	         return this.userSessionHandler.getUser();
	     }
	     
		public boolean login(User user) {
			User logged = userService.performLogin(user);
			if (logged!=null) {
				this.setUser(logged);
				return true;
			} else {
				return false;
			}
			
		}
		public boolean logout(User user) {
			this.setUser(null);
			return true;
		}
	     
	     
}
